﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.XPath;
using System.Xml;

namespace Paypayes.Aelda.Core
{
    public class SobManager
    {
        private Dictionary<UInt64, SimulationObject> _sobs;

        public SobManager()
        {
            _sobs = new Dictionary<ulong, SimulationObject>();
        }

        public void Add(SimulationObject newSob)
        {
            _sobs.Add(newSob.Id, newSob);
        }

        public SimulationObject LookupById(UInt64 id)
        {
            if (_sobs.ContainsKey(id))
            {
                return _sobs[id];
            }
            else
            {
                return null;
            }
        }

        public void Remove(SimulationObject sob)
        {
            _sobs.Remove(sob.Id);
        }

        public void Restore()
        {
            throw new NotImplementedException("à faire");
            // foreach Retore....
        }

        public XmlElement Store()
        {
            XmlDocument doc = new XmlDocument();
            XmlNode node;
            XmlElement root = doc.CreateElement("root");
           
            foreach (SimulationObject sob in _sobs.Values)
            {
                node= doc.CreateElement("sob");
                XmlAttribute keyAttr = doc.CreateAttribute("id");
                keyAttr.Value=sob.Id.ToString();
                node.Attributes.Append(keyAttr);
                root.AppendChild(node);

                sob.Store();
            }

            return root;

            throw new NotImplementedException("à faire");
            // foreach Store....
        }
    }
}
